/*
 * 文件名称: QueryShardisNode.java Copyright 2011-2016 Ximalaya All right reserved.
 */
package com.zxy.shardisdemo;

import com.ximalaya.shardis.hash.Hash;
import com.ximalaya.shardis.hash.MurmurHash;

/**
 * @author Bird Zhang
 */
public class QueryShardisNode {

  private static Hash murmurHash = new MurmurHash();

  private static int size = 256;

  private static String key = "1000748";

  public static int getShardNum() {
    int length = key.length();
    int start = -1;
    for (int k = 0; k != length; k++) {
      char indexChar = key.charAt(k);
      if (Character.isDigit(indexChar)) {
        start = k;
        break;
      }
    }
    if (start == -1)
      throw new IllegalArgumentException("Your key " + key + " does not hava any digit.");
    String trueKey = key.substring(start, length);
    long hash = murmurHash.hash(trueKey);
    int idx = Math.abs((int) (hash % size));
    return idx;
  }

  public static void main(String[] args) {
    System.out.println(getShardNum());
  }

}
